//
//  YFPersonDataCell.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/15.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFPersonDataCell.h"
#import "YFPersonModel.h"
#import "YFCustomTextField.h"

@interface YFPersonDataCell()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UILabel *valueTextLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleLabelBottomCon;

@property (nonatomic, strong) YFPersonModel *item;
//@property (nonatomic, assign) kPersonDataCellType cellType;

@end
@implementation YFPersonDataCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self configuerTextField];
    
    __weak typeof(self) weakSelf = self;
    [self.textField setTextChangeHandler:^(NSString *text) {
        if (weakSelf.textChangeHandler) {
            weakSelf.textChangeHandler(weakSelf.textField.text);
        }
    }];
    
    [self.textField setTextFieldDidEndEidtHandler:^(NSString *text) {
        [self showTextField:NO];
        if (self.endEdittextField) {
            self.endEdittextField(text);
        }
    }];

    [self.textField setTextFieldDidBenginEidtHandler:^(NSString *text) {
        if (self.beginEditTextField) {
            self.beginEditTextField(text);
        }
        [self showTextField:YES];
    }];
}

- (void)configuerTextField {
    self.textField.delegate = self;
    [self.textField changeTextfieldStyle:LSTextfieldViewTypeNormal];
    self.textField.backgroundColor = [UIColor clearColor];
    self.textField.autoAdjustAlimentCenterOrLeft = NO;
    self.textField.textAlignment = NSTextAlignmentLeft;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (self.clickKeyboardReturn) {
        self.clickKeyboardReturn(self.textField.text);
    }
    [self.textField resignFirstResponder];
    return YES;
}

- (void)configerCellWithItem:(YFPersonModel *)item {
    self.item = item;
    self.titleLabel.text = item.title;
    if (item.cellType == kPersonDataCellTextField) {
        self.textField.text = item.subTitle;
        self.valueTextLabel.text = @"";
        self.valueTextLabel.hidden = YES;
        self.textField.maxLength = item.textFieldMaxLength;
        self.textField.regExpCheck = item.regExpCheck;
        self.textField.keyboardType = item.keyBoardType;
        self.textField.hidden = NO;
        [self showTextField:(self.textField.text.length > 0)];
    }else if (item.cellType == kPersonDataCellAlert){
        self.valueTextLabel.text = item.subTitle;
        self.textField.text = @"";
        self.valueTextLabel.hidden = NO;
        self.textField.hidden = YES;
    }
}
// 显示或隐藏输入框
- (void)showTextField:(BOOL)show {
    if (self.item.cellType == kPersonDataCellTextField) {
        show = (self.textField.text.length > 0) ? YES:show;
        self.titleLabelBottomCon.constant = show ? 40:8;
        [UIView animateWithDuration:0.35 delay:0 usingSpringWithDamping:8 initialSpringVelocity:6 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.textField.alpha = show;
            self.titleLabel.font = show ? kOpenSansFont(OpenSansRegular, 12):kOpenSansFont(OpenSansRegular, 16);
            [self.textLabel layoutIfNeeded];
        } completion:^(BOOL finished) {
            self.textField.hidden = !show;
        }];
    }else {
        self.titleLabelBottomCon.constant = 8;
        self.textField.hidden = YES;
        self.titleLabel.font = kOpenSansFont(OpenSansRegular, 16);
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
    if (self.item.cellType == kPersonDataCellTextField && selected) {
        [self.textField becomeFirstResponder];
    }else {
        [self.textField resignFirstResponder];
    }
}

@end
